home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_gettext.idb / usr / freeware / bin / tupdate.z / tupdate
Encoding:
Text File  |  1999-04-16  |  6.0 KB  |  260 lines

  1. #! /usr/sbin/perl --                    # -*-Perl-*-
  2. eval "exec /usr/sbin/perl -S $0 $*"
  3.     if $running_under_some_shell;
  4.  
  5. # Combine a new .po file and an old translated .po file to a new translation.
  6. # Copyright (C) 1995 Free Software Foundation, Inc.
  7. # Ulrich Drepper <drepper@gnu.ai.mit.edu>, May 1995.
  8. #
  9.  
  10. # To be compatible with Perl4 we don't use localtime here.
  11. ;# ctime.pl is a simple Perl emulation for the well known ctime(3C) function.
  12. ;#
  13. ;# Waldemar Kebsch, Federal Republic of Germany, November 1988
  14. ;# kebsch.pad@nixpbe.UUCP
  15. ;# Modified March 1990, Feb 1991 to properly handle timezones
  16. ;#  $Id: tupdate.in,v 1.20 1995/08/23 21:13:11 drepper Exp $
  17. ;#   Marion Hakanson (hakanson@cse.ogi.edu)
  18. ;#   Oregon Graduate Institute of Science and Technology
  19. ;#
  20. ;# usage:
  21. ;#
  22. ;#     #include <ctime.pl>          # see the -P and -I option in perl.man
  23. ;#     $Date = &ctime(time);
  24.  
  25. CONFIG: {
  26.     package ctime;
  27.  
  28.     @DoW = ('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
  29.     @MoY = ('Jan','Feb','Mar','Apr','May','Jun',
  30.         'Jul','Aug','Sep','Oct','Nov','Dec');
  31. }
  32.  
  33. sub ctime {
  34.     package ctime;
  35.  
  36.     local($time) = @_;
  37.     local($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst);
  38.  
  39.     # Determine what time zone is in effect.
  40.     # Use GMT if TZ is defined as null, local time if TZ undefined.
  41.     # There's no portable way to find the system default timezone.
  42.  
  43.     $TZ = defined($ENV{'TZ'}) ? ( $ENV{'TZ'} ? $ENV{'TZ'} : 'GMT' ) : '';
  44.     ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) =
  45.         ($TZ eq 'GMT') ? gmtime($time) : localtime($time);
  46.  
  47.     # Hack to deal with 'PST8PDT' format of TZ
  48.     # Note that this can't deal with all the esoteric forms, but it
  49.     # does recognize the most common: [:]STDoff[DST[off][,rule]]
  50.  
  51.     if($TZ=~/^([^:\d+\-,]{3,})([+-]?\d{1,2}(:\d{1,2}){0,2})([^\d+\-,]{3,})?/){
  52.         $TZ = $isdst ? $4 : $1;
  53.     }
  54.     $TZ .= ' ' unless $TZ eq '';
  55.  
  56.     $year += ($year < 70) ? 2000 : 1900;
  57.     sprintf("%s %s %2d %2d:%02d:%02d %s%4d\n",
  58.       $DoW[$wday], $MoY[$mon], $mday, $hour, $min, $sec, $TZ, $year);
  59. }
  60. 1;
  61.  
  62. # Check for --help and --version first.
  63. #
  64. for ($cnt = 0; $cnt <= $#ARGV; $cnt++)
  65. {
  66.     if ($ARGV[$cnt] eq "--v"
  67.     || $ARGV[$cnt] eq "--ve"
  68.     || $ARGV[$cnt] eq "--ver"
  69.     || $ARGV[$cnt] eq "--vers"
  70.     || $ARGV[$cnt] eq "--versi"
  71.     || $ARGV[$cnt] eq "--versio"
  72.     || $ARGV[$cnt] eq "--version")
  73.     {
  74.     print "tupdate - gettext 0.10\n";
  75.     exit 0;
  76.     }
  77.     if ($ARGV[$cnt] eq "--h"
  78.     || $ARGV[$cnt] eq "--he"
  79.     || $ARGV[$cnt] eq "--hel"
  80.     || $ARGV[$cnt] eq "--help")
  81.     {
  82.     print "tupdate [--help | --version | NEW OLD]\n";
  83.     print "  --help      give this help message and exit\n";
  84.     print "  --version   display version information and exit\n";
  85.     print "\n";
  86.     print "NEW is the last created PO file (generally by xgettext).\n";
  87.     print "It need not contain any translations.  OLD is the PO file\n";
  88.     print "including the old translations which will be taken over to\n";
  89.     print "the newly created file as long as they still match.\n";
  90.     exit 0;
  91.     }
  92. }
  93.  
  94. # We take exactly two arguments.
  95. #
  96. if ($#ARGV != 1)
  97. {
  98.     die "Try `tupdate --help' for more information.\n";
  99. }
  100.  
  101. unless (open(OLD, "$ARGV[1]"))
  102. {
  103.     die "Cannot open old translated .po file '$ARGV[1]'\n";
  104. }
  105.  
  106. $last_was_special = 0;
  107. $have_special = 0;
  108. $last_comment = "";
  109. $comment_special = "";
  110.  
  111. while (<OLD>)
  112. {
  113.     if (/^msgid[ \t]*/)
  114.     {
  115.     $_ =~ s/^msgid[ \t]*//;
  116.     $msgid = $_;
  117.  
  118.     $_ = <OLD>;
  119.     $last_was_special = 1
  120.         unless ($msgid ne "\"\"\n" || /^"/);
  121.  
  122.         while (/^"/)
  123.         {
  124.         $msgid = "$msgid$_";
  125.             $_ = <OLD>;
  126.         }
  127.     }
  128.     if (/^msgstr/)
  129.     {
  130.         $_ =~ s/^msgstr[ \t]*//;
  131.         $msgstr = $_;
  132.  
  133.         $_ = <OLD>;
  134.         while (/^"/)
  135.         {
  136.         $msgstr = "$msgstr$_";
  137.             $_ = <OLD>;
  138.         }
  139.  
  140.     if ($last_was_special == 0)
  141.     {
  142.         # It makes no sense to keep entries without translations.
  143.             $translate{$msgid} = $msgstr
  144.                 if ($msgstr ne "\"\"\n");
  145.             $comment{$msgid} = $last_comment
  146.                 if ($last_comment ne "");
  147.             $last_comment = "";
  148.     }
  149.     else
  150.     {
  151.         $special_entry = $msgstr;
  152.         $have_special = 1;
  153.         $last_was_special = 0;
  154.             $comment_special = $last_comment;
  155.             $last_comment = "";
  156.     }
  157.     }
  158.     $last_comment = "$last_comment$_"
  159.     if (/^#[ \n\t]/);
  160. }
  161.  
  162. close (OLD);
  163.  
  164. unless (open (NEW, "$ARGV[0]"))
  165. {
  166.     die "Cannot open new .po file '$ARGV[0]'\n";
  167. }
  168.  
  169. # to be sure
  170. $last_was_special = 0;
  171. $first_msgstr_output = 1;
  172.  
  173. while (<NEW>)
  174. {
  175.     if (/^msgid[ \t]*/)
  176.     {
  177.     $_ =~ s/^msgid[ \t]*//;
  178.     $msgid = $_;
  179.  
  180.     $_ = <NEW>;
  181.     $last_was_special = 1
  182.         unless ($msgid ne "\"\"\n" || /^"/);
  183.  
  184.         while (/^"/)
  185.         {
  186.         $msgid = "$msgid$_";
  187.             $_ = <NEW>;
  188.         }
  189.     }
  190.     if (/^msgstr/)
  191.     {
  192.         $_ =~ s/^msgstr[ \t]*//;
  193.         $msgstr = $_;
  194.  
  195.         $_ = <NEW>;
  196.         while (/^"/)
  197.         {
  198.         $msgstr = "$msgstr$_";
  199.             $_ = <NEW>;
  200.         }
  201.  
  202.     if ($last_was_special != 0)
  203.     {
  204.         # The new translated .po file should contain the special
  205.         # information entry of the old translated file.  But if
  206.         # later does not contain one we generate it by copying
  207.         # from the new .po file.
  208.         print $comment_special;
  209.  
  210.             print "msgid $msgid";
  211.  
  212.         $special_entry = $msgstr
  213.         if ($have_special == 0);
  214.  
  215.         $today = &ctime (time);
  216.         chop $today;
  217.         $special_entry =~
  218.            s/Updated: .*/Updated: $today\\n"/;
  219.         printf "msgstr $special_entry";
  220.  
  221.         $last_was_special = 0;
  222.     }
  223.     elsif (defined $translate{$msgid})
  224.     {
  225.             print $comment{$msgid}
  226.         if (defined ($comment{$msgid}));
  227.  
  228.         print "msgid $msgid";
  229.  
  230.             print "msgstr $translate{$msgid}";
  231.         delete $translate{$msgid}
  232.     }
  233.         else
  234.         {
  235.         print "msgid $msgid";
  236.             print "msgstr $msgstr";
  237.         }
  238.     }
  239.  
  240.     print;
  241. }
  242.  
  243. foreach $key (keys %translate)
  244. {
  245.     print $comment{$key}
  246.     if (defined ($comment{$key}));
  247.     $escaped = $key;
  248.     chop $escape;
  249.     $escaped =~ s/\n/\n# /g;
  250.     print "# msgid $escaped";
  251.     $escaped = $translate{$key};
  252.     $escaped =~ s/\n/\n# /g;
  253.     print "msgstr $escaped\n";
  254. }
  255.  
  256. # This variable is still set from processing the old .po file.
  257. print $last_comment;
  258.  
  259. exit 0;
  260.